/*
    Copyright (c) <year>, <copyright holder>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
        * Neither the name of the <organization> nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef DATASET_H
#define DATASET_H

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <assert.h>
#include <fstream>
#include <cstdlib>

#include "codebook.h"

static const int VERBOSE = 2;
static const std::string PATH_BASE = "/media/data/base";

/*!
* @struct Paths
* @brief Paths structure
*/
typedef struct path
{
    std::string root;
    std::string images;
    std::string features;
    std::string codebook;
    std::string classifier;
} Paths;

/*!
* @struct DataSetParams
* @brief General dataset parameters
*/
typedef struct datasetparams
{
    unsigned int num_img;
    unsigned int num_cls;
    Paths paths;
} DataSetParams;

/*!
* @struct ClassifParams
* @brief Classification parameters
*/
typedef struct classifparams
{
    unsigned int num_tr;
    unsigned int num_ts;
    unsigned int num_cv;
} ClassifParams;

/*!
* @struct ImagefParams
* @brief Image parameters
*/
typedef struct imgparams
{
    unsigned int max_image_size;
} ImageParams;

/*!
* @struct SIFTParams
* @brief Sift parameters
*/
typedef struct siftparams
{
    unsigned int grid_spacing;
    unsigned int patch_size;
    unsigned int bin_size;
    unsigned int num_scales;
    float scale_step;
    std::vector<unsigned int> bin_size_range;
} SIFTParams;




/*!
* @struct ImgInfo
* @brief Image iformations
*/
typedef struct iminfo
{
    unsigned int                idx; /*!< the index of the image within the dataset */
    int                 		label; /*!< the label of the current image */
    std::string         		file_name;/*!< the full file name */
    std::vector<int>    		vect_labels; /*!< binary vector of the ossciated labels */
} ImgInfo;

/*!
* @class Dataset
* @brief Abstract Dataset Class
*/
class DataSet
{
protected:
    std::string 				m_name;  		/*!< dataset name  */
    std::string 				m_file_data;  	/*!< output file name for data */
    std::vector<std::string> 	m_cls_names; 	/*!< list of class names */
	std::map<int, ImgInfo>		m_img;			/*!< image list infos */

	Codebook 					m_codebook;		/*!< codebook */
	
	
    DataSetParams 	m_dataset_params; /*!< General dataset params */
    ImageParams 	m_img_params; /*!< Image params */
    SIFTParams 		m_sift_params; /*!< Sift params */
    
    ClassifParams 	m_classif_params; /*!< Classification params */

	

public:

    /*!
    * @fn DataSet ()
    * @brief Constructor
    */
    DataSet () {m_codebook = Codebook();}

    /*!
    * @fn ~DataSet ()
    * @brief Virtual Destructor
    */
    virtual ~DataSet () {}

	/*!
	* @fn virtual Init (bool loading = false,  std::string out_file_name = "", bool with_saving = true) = 0 ;
	* @brief pure virtual Initialise dataset function
	* @param bool loading loading from a file (default = 0)
	* @param std::string out_file_name file for loading or saving data (default = m_file_data)
	* @param bool with_saving saving option into a file (default = 1)
	*/
	virtual int init (bool loading = false,
					  std::string out_file_name = "",
					  bool with_saving = true) = 0;

	int doCodebook ();
	

	/*!
    * @fn std::map<int, ImgInfo> getListImgInfos () const
    * @brief Const function to get the list of image infos
    * @return td::map<int, ImgInfo> list of images infos
    */
    std::map<int, ImgInfo> getListImgInfos () const {
        return m_img;
    }

    /*!
    * @fn std::vector<std::string> getListClasses () const
    * @brief Const function to get the lis of class names
    * @return std::vector<std::string> list of class names
    */
    std::vector<std::string> getListClasses () const {
        return m_cls_names;
    }
    
  protected:
	
    /*!
    * @fn void setParams () = 0
    * @brief Pure Virtual setParams function
    */
    virtual void setParams () = 0;

    
    /*!
    * @fn int setListClasses () = 0
    * @brief Pure Virtual setParams function
    * @return operation success if = 0
    */
    virtual int setListClasses () = 0;

    /*!
    * @fn int setListImageFiles ( const std::string ext = "jpg") = 0
    * @brief Pure virtual function to list dataset image file names
    * @return operation success if = 0
    */
    virtual int setListImageFiles ( const std::string ext = "jpg") = 0;

};





#endif // DATASET_H
